<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>案例-鼠标拖拽</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        #box{
            width: 100px;
            height: 100px;
            position: absolute;
            background: yellow;
            left: 0;
        }
    </style>
</head>
<body>
    <div id="box"></div>
    <script>
        box.onmousedown = function(evt){
            document.onmousemove = function(devt){
                var x = devt.clientX - evt.offsetX
                if(x < 0){
                    x = 0
                }
                console.log(document.documentElement.clientWidth, parseInt(getComputedStyle(box).left), parseInt(getComputedStyle(box).width))
                if((document.documentElement.clientWidth - parseInt(getComputedStyle(box).left)) < parseInt(getComputedStyle(box).width)){
                    x = document.documentElement.clientWidth -  parseInt(getComputedStyle(box).width)
                }
                box.style.left = x + 'px'
                // box.style.left = x + 'px'
                var y = devt.clientY - evt.offsetY
                if(y < 0){
                    y = 0
                }
                box.style.top = y + 'px'

            }
        }
        document.onmouseup = function(){
            document.onmousemove = null
        }
    </script>
</body>
</html>